from rest_framework import serializers
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import CreateModelMixin,ListModelMixin,RetrieveModelMixin, DestroyModelMixin,UpdateModelMixin

from api.serializers.hosts import Hostserializer
from api.serializers.oauth import AssetsAdminSerializer
from utils.views import ChoiceAPIView
from utils.Mixins import  MultipleDestroyMixin

from asset.models import Host,Region
from oauth.models import Departments

from rest_framework.filters import SearchFilter, OrderingFilter
from rest_framework.generics import ListAPIView

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from django.contrib.auth import get_user_model
Users = get_user_model()

from utils.custom_log   import log_start
logger = log_start('host')


class HostsMultipleDestroy(APIView):
    
    def delete(self,request, *args, **kwargs):
        delete_ids = self.request.query_params.get('ids', None)
        logger.info(f'批量删除id:{delete_ids}')
        if not delete_ids:
            return Response(data={'detail': '参数错误, ?ids=x,x 为必传参数'}, status=status.HTTP_400_BAD_REQUEST)
        
        delete_ids = delete_ids.split(',')
        del_queryset = Host.objects.filter(id__in=delete_ids)
        if len(delete_ids) != del_queryset.count():
            return Response(data={'detail': '删除数据不存在'}, status=status.HTTP_400_BAD_REQUEST)
        
        del_queryset.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)





class HostsViewSet(CreateModelMixin, ListModelMixin, GenericAPIView):
    queryset = Host.objects.all()
    serializer_class = Hostserializer
    filter_backends = [SearchFilter, OrderingFilter]
    search_fields = ['hostname', 'ip', 'instance_id','floatingip']
    ordering_fields = ['id', 'hostname']
    
    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)


class HostsDetailView(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):
    
    queryset = Host.objects.all()
    serializer_class = Hostserializer
    
    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)
    

class ServerStatusAPIView(ChoiceAPIView):
    """
    get:
    服务器--models类型列表

    服务器models中的类型列表信息, status: 200(成功), return: 服务器models中的类型列表
    """
    choice = Host.asset_status_choice

class ServerTypeAPIView(ChoiceAPIView):
    """
    get:
    服务器--models类型列表

    服务器models中的类型列表信息, status: 200(成功), return: 服务器models中的类型列表
    """
    choice = Host.asset_type_choice



class ServerIPListAPIView(APIView):
    """
    get:
    ip 列表
    
    IP 列表信息, status: 200(成功), return: 权限models中Department列表信息
    """
    def get(self, request):
        lst =  [ {'label': x.ip ,'value' : x.id } for x in Host.objects.all() ]
        return Response(data={'results':lst})


class ServerDepartmentListAPIView(APIView):
    """
    get:
    Department列表
    
    Department列表信息, status: 200(成功), return: 权限models中Department列表信息
    """
    def get(self, request):
        lst =  [ {'label': x.name ,'value' : x.id } for x in Departments.objects.all() ]
        return Response(data={'results':lst})

class ServerRegionListAPIView(APIView):
    """
    get:
    Region列表
    
    Region列表信息, status: 200(成功), return: 权限models中Region列表信息
    """
    def get(self, request):
        lst =  [ {'value' : x.id,'label' : x.region} for x in Region.objects.all() ]
        return Response(data={'results':lst})


class AssetsAdminListAPIView(ListAPIView):
    """
    get:
    资产--管理员查询

    资产管理员列表信息, status: 200(成功), return: 资产管理员列表信息
    """
    queryset = Users.objects.all()
    serializer_class = AssetsAdminSerializer
    filter_backends = (SearchFilter,)
    search_fields = ['username']